package cn.kailangtu.dataspot.activify;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.kailangtu.commonevents.eventEntity.KeyboardConfirmActionEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardConfirmResultEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardNumberActionEvent;
import cn.kailangtu.dataspot.data.consts.KeyboardAction;
import cn.kailangtu.dataspot.data.context.KeyboardConfirmContext;
import cn.kailangtu.dataspot.data.context.KeyboardContext;
import cn.kailangtu.dataspot.databinding.FragmentKeyboardConfirmBinding;
import cn.kailangtu.dataspot.databinding.FragmentKeyboardNumberBinding;

public class KeyboardConfirmFragment extends KeyboardBaseFragment {

    private FragmentKeyboardConfirmBinding binding;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentKeyboardConfirmBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }




    private void initListener(){

        binding.btnConfirmCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new KeyboardConfirmResultEvent(false));
            }
        });
        binding.btnConfirmSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new KeyboardConfirmResultEvent(true));
            }
        });
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(KeyboardConfirmActionEvent event){

        keyboard(event.getActionValue());
    }

    private void keyboard(int action ){
        switch (action) {
            case KeyboardAction.OPEN:
                binding.tvKeyboardConfirmMsg.setText(KeyboardConfirmContext.tipMsg);
                KeyboardConfirmContext.setSelectedView(binding.btnConfirmCancel);
                break;
            case KeyboardAction.ENTRY:
                KeyboardConfirmContext.entry();
               // binding.tvKeyboardResult.setText(KeyboardContext.result);
                break;
            case KeyboardAction.LEFT:
            case KeyboardAction.RIGHT:

                if(KeyboardConfirmContext.getSelectedView() == null){
                    KeyboardConfirmContext.setSelectedView(binding.btnConfirmCancel);
                }
                else if(KeyboardConfirmContext.getSelectedView().equals(binding.btnConfirmCancel))
                {
                    KeyboardConfirmContext.setSelectedView(binding.btnConfirmSave);
                }else if(KeyboardConfirmContext.getSelectedView().equals(binding.btnConfirmSave)){
                    KeyboardConfirmContext.setSelectedView(binding.btnConfirmCancel);
                }
                else {
                    KeyboardConfirmContext.setSelectedView(binding.btnConfirmCancel);
                }
                break;
        }
    }
}
